<template>
  <view class="container">
    <view class="logo">
      <!-- 这里可以放你的应用logo -->
      <image src="https://bigironcollision.obs.cn-east-3.myhuaweicloud.com:443/temp%2Ftx_1757241357597.png" mode="aspectFit"></image>
    </view>
    <!-- <image src="https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132" mode=""></image> -->
    <view class="login-btn" @click="handleWechatLogin">
      微信授权登录
    </view>
    
    <view class="tip-text" style="display: flex;">
		<checkbox-group @change="changeNotescheckbox">
			<label class="uni-list-cell uni-list-cell-pd" >
				<view>
					<checkbox :checked="ckpurchaseNotes" value="1"/>
							<text>请仔细阅读并勾选同意</text>
				</view>
				<view></view>
			</label>
		</checkbox-group>
		
      <text class="protocol" @click="goyonghu">《用户协议》</text>和<text class="protocol" @click="goyins">《隐私政策》</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 可以在这里添加需要的数据
	  openid:'',
	  shareId:'',
	  ckpurchaseNotes:'',
    }
  },
  onLoad(opt) {
  	if(opt.shareId){
		this.shareId=opt.shareId
	}
  },
  methods: {
	  changeNotescheckbox(e){
	  	var values = e.detail.value;
	  	 if(values.length){
	  		this.ckpurchaseNotes=true 
	  	 }else{
	  		this.ckpurchaseNotes=false
	  	 }
	  },
	goyonghu(){
	  uni.navigateTo({
	  	url:'../user/agreement'
	  })	
	},
	goyins(){
		uni.navigateTo({
			url:'../user/privacy'
		})
	},
    // 处理微信登录
    handleWechatLogin() {
		if(!this.ckpurchaseNotes){
				  uni.showToast({
				      title:'请先仔细阅读并先勾选用户协议跟隐私协议',
				  	   icon:'none',
				      duration: 3000,
				  })
				  return false
		}
      // #ifdef MP-WEIXIN
      uni.login({
        provider: 'weixin',
        success: (loginRes) => {
          console.log('微信登录成功', loginRes);
          // 这里可以获取到code，需要发送到你的后端服务器换取openid和session_key
          const code = loginRes.code;
          
          // 获取用户信息
          uni.getUserInfo({
            provider: 'weixin',
            success: (infoRes) => {
              console.log('用户信息', infoRes);
              // 这里可以获取用户信息，如昵称、头像等
              const userInfo = infoRes.userInfo;
              
              // 通常这里会调用后端API，将code和userInfo发送到服务器
               this.loginToServer(code, userInfo);
              
              // 登录成功后的操作，比如跳转到首页
             /* uni.showToast({
                title: '登录成功',
                icon: 'success'
              }); */
              /* uni.switchTab({
                url: '/pages/index/index'
              }); */
            },
            fail: (err) => {
              console.log('获取用户信息失败', err);
              uni.showToast({
                title: '获取用户信息失败',
                icon: 'none'
              });
            }
          });
        },
        fail: (err) => {
          console.log('微信登录失败', err);
          uni.showToast({
            title: '登录失败',
            icon: 'none'
          });
        }
      });
      // #endif
      
      // #ifndef MP-WEIXIN
      uni.showToast({
        title: '请在微信小程序中打开',
        icon: 'none'
      });
      // #endif
    },
    
    // 实际项目中，这里应该调用后端API进行登录
    loginToServer(code, userInfo) {
      // 示例代码，实际项目中需要替换为你的API调用
     console.log(code)
	 uni.showLoading({
	 	title: '加载中'
	 })
	 this.$http('jeecg-boot/api/wx/getOpeId',{
	 	js_code:code
	 },'POST').then(res => {
	 	 console.log(res.result)
		 this.openid=res.result
		 uni.setStorageSync('openid',this.openid ) 
	 	// uni.hideLoading();
	 	 this.wxLogin()
	  })
	 //console.log(userInfo)
	/* uni.request({
	    
	     url: 'https://api.weixin.qq.com/sns/jscode2session',
	     data: {
			  appid: 'wx656d092b8ad08d02',
			  secret: 'bcac713f9e551ec484f0d05f8f258581',
			  js_code: code,
			  grant_type: 'authorization_code'
		 },
	    // header: header,
	     dataType: 'json',         
	 }).then((response) => {
		 console.log(response.data)
	 }) */
    },
	wxLogin(){
		this.$http('jeecg-boot/sys/wxLogin',{
			username:this.openid,
			sharePerson:this.shareId
		},'POST').then(res => {
			
			 uni.hideLoading();
			if(res.success){
				 uni.showToast({
					 title: '登录成功',
					 icon:'none',
					 duration: 3000,
				 });
				 var userInfo=res.result.userInfo
				 var token=res.result.token
				 uni.setStorageSync('userInfo', userInfo)
				uni.setStorageSync('token',token ) 
				 // 登录成功后跳转到首页
				  uni.switchTab({
					url: '/pages/index/index'
				  }) 
			}else{
				 uni.showToast({
					 title: res.message,
					 icon:'none',
					 duration: 3000,
				 });
			}
		 })
	}
  }
}
</script>

<style lang="scss">
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 40rpx;
  box-sizing: border-box;
}

.logo {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 100rpx;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.login-btn {
  width: 600rpx;
  height: 90rpx;
  line-height: 90rpx;
  text-align: center;
  background-color: #FF6B81; /* 浅红色 */
  color: #FFFFFF;
  border-radius: 45rpx;
  font-size: 32rpx;
  margin-bottom: 40rpx;
  box-shadow: 0 10rpx 20rpx rgba(255, 107, 129, 0.3);
}

.tip-text {
  font-size: 24rpx;
  color: #999999;
  
  .protocol {
    color: #576B95;
  }
}
</style>